package com.raindrop.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * XmlHelper
 * xml工具类
 *
 * @author liuhang
 * @date 2018/5/16
 */
public class XmlUtil {
    /**
     * 转换工具  xml转map
     *
     * @param inputStream
     * @return
     * @throws DocumentException
     * @throws IOException
     */
    public static Map xml2Map(InputStream inputStream) throws DocumentException, IOException {
        Map<String, String> map = new HashMap<>();
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();
        for (Element e : elementList)
            map.put(e.getName(), e.getText());
        inputStream.close();
        return map;
    }

    /**
     * Map 转 XML
     *
     * @param map
     * @return
     */
    public static String MapToXML(Map map, String key) {
        return MapToXML(map, key, null);
    }

    public static String MapToXML(Map map, String key, String... hideStrs) {
        System.out.println("将Map转成Xml, Map：" + map.toString());
        StringBuffer sb = new StringBuffer();
        sb.append("<" + key + ">");
        mapToXML(map, sb, hideStrs);
        sb.append("</" + key + ">");
        System.out.println("将Map转成Xml, Xml：" + sb.toString());
        try {
            return new String(sb.toString().getBytes("UTF-8"));
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    private static void mapToXML(Map map, StringBuffer sb) {
        mapToXML(map, sb, null);
    }

    private static void mapToXML(Map map, StringBuffer sb, String[] hideStrs) {
        Set set = map.keySet();
        for (Iterator it = set.iterator(); it.hasNext(); ) {
            String key = (String) it.next();
            Object value = map.get(key);
            List<String> strs = Arrays.asList(hideStrs);
            if (null == value)
                value = "";
            if (value.getClass().getName().equals("java.util.ArrayList")) {
                ArrayList list = (ArrayList) map.get(key);
                sb.append("<" + key + ">");
                for (int i = 0; i < list.size(); i++) {
                    HashMap hm = (HashMap) list.get(i);
                    mapToXML(hm, sb);
                }
                sb.append("</" + key + ">");
            } else if (strs.contains(key)) {
                continue;
            } else {
                if (value instanceof HashMap) {
                    sb.append("<" + key + ">");
                    mapToXML((HashMap) value, sb);
                    sb.append("</" + key + ">");
                } else if (value instanceof String) {
                    sb.append("<" + key + "><![CDATA[" + value + "]]></" + key + ">");
                } else {
                    sb.append("<" + key + ">" + value + "</" + key + ">");
                }
            }
        }
    }
}
